<?php
/**
 * Created by PhpStorm.
 * User: linyh
 * Date: 2017/11/25 0025 10:13
 */
?>
<link href="/ueditor/themes/default/css/umeditor.css" type="text/css" rel="stylesheet" />
<section class="content-header">
    <h1>
        公告发布
    </h1>
</section>

<!-- Main content -->
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <!-- general form elements -->
            <div class="box box-default">
                <!-- /.box-header -->
                <!-- form start -->
                <form id="form" role="form" class="form-horizontal">
                    <input type="hidden" value="2" name="type">
                    <div class="box-body">
                        <div class="form-group">
                            <label for="input1" class="col-sm-2 control-label">公告标题</label>
                            <div class="col-sm-9">
                            <input id="input1" class="form-control" type="text" value="" name="title">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="input2" class="col-sm-2 control-label">公告对象</label>
                            <div class="row col-sm-9">
                                <div class="col-md-3">
                                    <select id="input2" class="form-control" name="country_id">
                                        <option value="">所有国家</option>
                                    </select>
                                </div>
                                <div class="col-md-3">
                                    <select id="input2-2" class="form-control col-md-6" name="role">
                                        <option value="">所有实体</option>
                                        <option value="1">生产商</option>
                                        <option value="2">贸易商</option>
                                        <option value="3">政府</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="input3" class="col-sm-2 control-label">公告内容</label>
                            <div class="col-sm-8">
                            <script type="text/plain" name="content" id="myEditor" style="width:800px;height:240px;">
                                <p>这里输入公告内容</p>
                            </script>
                            <!--</script>-->
                            </div>
                        </div>
                    </div>
                    <!-- /.box-body -->

                    <div class="box-footer text-center">
                        <button type="submit" class="btn btn-success">发布</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</section>

<script type="text/javascript" charset="utf-8" src="/ueditor/third-party/template.min.js"></script>
<script type="text/javascript" charset="utf-8" src="/ueditor/umeditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="/ueditor/umeditor.min.js"></script>
<script type="text/javascript" charset="utf-8" src="/ueditor/lang/zh-cn/zh-cn.js"></script>


<script>
var um = UM.getEditor('myEditor');
$('#form').submit(function(e) {
    e.preventDefault();
    var str = $('#form').serialize();
    console.log(str);

    $.ajax('/api/news/create', {
        type: 'POST',
        data: str,
        dataType: 'json',
        headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
    }).then(function(ret) {
        if(ret.state ==0) {
            alert('保存成功');
            location.href = '/news/notice';
        } else {
            alert(ret.message);
        }
    });
});

$(function() {
    if(global.user.role == 4) {
        $.ajax('/api/country/list', {
            type: 'get',
            data: {
            },
            dataType: 'json',
            headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
        }).then(function (ret) {
            for (var i in ret.data.list) {
                $('#input2').append($('<option>').attr('value', ret.data.list[i].id).text(ret.data.list[i].name));
            }
        });
    } else {
        $('#input2').parent().remove();
    }
})

</script>